<template>
	<view class="container">
		<!-- 消息列表 -->
		<view class="msg-list">
			<view class="item" @click="handleOpenPlatMessage">
				<image src="http://images.linglinggong.net/icon1/54.png" mode="aspectFill"></image>
				<text class='circle' v-if="isNewPlatMsg"></text>
				<view style="width: 100%;">
					<text>平台消息</text>
					<text
						style="width: 95%;overflow: hidden;text-overflow: ellipsis;white-space: nowrap;">{{ lastPlatMsg.content.title }}</text>
				</view>
				<text class="time">{{ lastPlatMsgTime }}</text>
			</view>
		</view>
	</view>
</template>
<script>
	import formatDate from '@/libs/time-date.js'
	import uniLoadMore from '@/components/uni-load-more/uni-load-more.vue'
	export default {
		components: {
			uniLoadMore,
		},
		data() {
			return {
				lastPlatMsg: {
					content: {
						titleText: ''
					}
				}, //最后一条平台消息
				lastPlatMsgTime: '', //最后一条平台消息时间
				requestObj: { //
					pageNum: 1,
					pageSize: 1,
				},
			}
		},
		computed: {
			'isNewPlatMsg': function() {
				return this.$store.state.isNewPlatMsg
			},
		},
		methods: {
			// 获取平台消息列表
			async get_push_msg_list() {
				let res = await this.$fetch(this.$api.get_push_msg_list, this.requestObj, 'POST')
				if (res.code) {
					// if(res.msg=='未登录'){
					// 	uni.navigateTo({
					// 	url:'/pages/login/login'
					// })
					// }
					return uni.showToast({
						title: res.msg,
						icon: 'none'
					})
				}
				//console.log(res)
				if (res.total > 0) {
					res.rows[0].content = JSON.parse(res.rows[0].content)
					this.lastPlatMsg = res.rows[0]
					this.lastPlatMsgTime = this.lastPlatMsg.createTime
					let currentTime = formatDate(new Date().getTime())
					let yesterday = formatDate(new Date().setDate(new Date().getDate() - 1))
					if (this.lastPlatMsgTime.slice(0, 11) == currentTime.slice(0, 11)) {
						this.lastPlatMsgTime = this.lastPlatMsgTime.slice(11, 16)
					} else if (this.lastPlatMsgTime.slice(0, 11) == yesterday.slice(0, 11)) {
						this.lastPlatMsgTime = '昨天'
					} else {
						this.lastPlatMsgTime = this.lastPlatMsgTime.slice(0, 11)
					}
				}
			},
			//打开平台消息页面
			handleOpenPlatMessage() {
				if (!uni.getStorageSync('token')) {
					uni.reLaunch({
						url: '/pages/login/login'
					})
				} else {
					uni.navigateTo({
						url: "/pages/platform-message/platform-message"
					})
					// 打开平台消息，是平台消息红点
					this.$store.commit('isNewPlatMsgChange', false)
				}
			},
		},
		onLoad() {
			uni.hideTabBar()
			this.get_push_msg_list() //平台消息
		},
		onShow() {},
		onPullDownRefresh() {
			//监听用户下拉动作，一般用于下拉刷新
			//this.getMsgList()
			//this.get_push_msg_list()
		},
	}
</script>
<style lang="less" scoped>
	.container {
		position: relative;
		overflow: hidden;

		.msg-list {
			margin-top: 30rpx;

			.item {
				padding: 26rpx 30rpx 24rpx;
				background: #FFFFFF;
				display: flex;
				position: relative;

				&>.icon {
					align-self: center;
					// border: 1px solid #000000;
					margin-left: 20rpx;
					margin-right: 30rpx;
				}

				&>image {
					max-width: 90rpx;
					min-width: 90rpx;
					max-height: 90rpx;
					min-height: 90rpx;
					margin-right: 30rpx;
					border-radius: 50%;
					background-color: #f3f3f3;
				}

				&>.circle {
					position: absolute;
					top: 30rpx;
					left: 110rpx;
					background-color: #FF4500;
					font-size: 12px;
					color: #FFFFFF;
					width: 20rpx;
					height: 20rpx;
					border-radius: 50%;
					display: flex;
					align-items: center;
					justify-content: center;
				}

				&>view {
					display: flex;
					flex-direction: column;

					text:first-child {
						font-weight: bold;
						margin-bottom: 10rpx;
						height: 40rpx;
						letter-spacing: 0.17px;
					}

					text:last-child {
						height: 34rpx;
						font-size: 12px;
						letter-spacing: 0.14px;
						color: rgba(64, 33, 16, 0.50);
					}
				}

				&>.time {
					position: absolute;
					top: 30rpx;
					right: 30rpx;
					font-size: 12px;
					color: #9B9B9B;
					letter-spacing: 0.14px;
				}

				&::before {
					content: "";
					position: absolute;
					width: 600rpx;
					height: 1px;
					background: #F2F2F2;
					right: 0rpx;
					bottom: 0;
				}
			}
		}

		.button-handle {
			display: flex;
			align-items: center;
			position: fixed;
			width: 100vw;
			height: 100rpx;
			background: #FFFFFF;
			bottom: 0rpx;

			&>view:first-child {
				width: 192rpx;
				display: flex;
				align-items: center;
				height: 100rpx;

				icon {
					margin-left: 30rpx;
					margin-right: 20rpx;
				}
			}

			&>.invitation-go {
				background: #FCC928;
				height: 100rpx;
				display: flex;
				justify-content: center;
				align-items: center;
				flex: 1;

				&:active {
					opacity: 0.7;
				}
			}
		}
	}
</style>
